import request from '@/utils/request'
import { submitDealReq } from './auth'
import { getRemoteUrlParam } from '@/utils/hyurl'

export function fetchExportData(thisObj, urlKey, params = {}, refExport = {}) {
  const exportTmpData = {}
  exportTmpData.S_PAGE_ID = refExport.refExportObjPageId
  exportTmpData.S_PAGE_VER = refExport.refExportObjPageVer
  exportTmpData.S_PARTITION = refExport.refExportObjPartition

  const exportTmpParam = {}
  exportTmpParam.S_DY_PARAM = refExport.refExportObjUrlParam
  const newUrlKey = 'export.' + urlKey
  thisObj.dealApi[newUrlKey] = exportTmpData
  thisObj.dealParam[newUrlKey] = exportTmpParam
  const postParam = getRemoteUrlParam(thisObj, newUrlKey, thisObj.globalData)
  postParam.refExportUrlKey = refExport.refExportUrlKey
  postParam.refExportPkId = refExport.refExportPkId
  postParam.viewKey = refExport.viewKey
  let iPageNo = 1
  let iPageSize = 100
  if (thisObj.tablePagination !== undefined) {
    iPageNo = thisObj.tablePagination.current
    iPageSize = thisObj.tablePagination.pageSize
  }
  postParam.iPageNo = iPageNo
  postParam.iPageSize = iPageSize
  thisObj.loading = true
  submitDealReq(postParam).then((res) => {
    thisObj.loading = false
    if (res.data.success) {
      const expData = res.data.data
      const url = '/autodeal/file/ctr/export/' + expData.S_ID
      request.download(url, {}, expData.S_FILE_NAME)
    } else {
      thisObj.$message.warning(res.data.message)
    }
  }).catch(reason => {
    console.log('reason' + reason)
  })
}

export function fetchExportFtpData(thisObj, urlKey, params = {}, refExport = {}) {
  const exportTmpData = {}
  exportTmpData.S_PAGE_ID = refExport.refExportObjPageId
  exportTmpData.S_PAGE_VER = refExport.refExportObjPageVer
  exportTmpData.S_PARTITION = refExport.refExportObjPartition

  const exportTmpParam = {}
  exportTmpParam.S_DY_PARAM = refExport.refExportObjUrlParam
  const newUrlKey = 'export.' + urlKey
  thisObj.dealApi[newUrlKey] = exportTmpData
  thisObj.dealParam[newUrlKey] = exportTmpParam
  const postParam = getRemoteUrlParam(thisObj, newUrlKey, thisObj.globalData)
  postParam.refExportUrlKey = refExport.refExportUrlKey
  postParam.refExportPkId = refExport.refExportPkId
  postParam.viewKey = refExport.viewKey
  postParam.I_EXPORT_OBJ = refExport.I_EXPORT_OBJ
  postParam.pushDynamicKey = refExport.pushDynamicKey
  let iPageNo = 1
  let iPageSize = 100
  if (thisObj.tablePagination !== undefined) {
    iPageNo = thisObj.tablePagination.current
    iPageSize = thisObj.tablePagination.pageSize
  }
  postParam.iPageNo = iPageNo
  postParam.iPageSize = iPageSize
  thisObj.loading = true
  submitDealReq(postParam).then((res) => {
    if (res.data.success) {
      const expData = res.data.data
      const url = '/autodeal/file/ctr/export/' + expData.S_ID
      request.download(url, {}, expData.S_FILE_NAME)
      thisObj.loading = false
    } else {
      thisObj.$message.warning(res.data.message)
    }
  }).catch(reason => {
    console.log('reason' + reason)
  })
}
